package com.xr.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xr.constant.MessageConstant;
import com.xr.exception.CustomException;
import com.xr.pojo.CheckGroup;
import com.xr.query.QueryPageBean;
import com.xr.result.PageResult;
import com.xr.result.Result;
import com.xr.service.CheckGroupService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.sound.midi.Soundbank;
import java.util.List;

@RestController
@RequestMapping("checkGroup")
public class CheckGroupController {

    //    遠程訂閲服務 注解注入
    @Reference
    private CheckGroupService checkGroupService;
//    查询所有检查组
    @RequestMapping("/queryAllCheckGroup")
    public Result queryAllCheckGroup(){
        List<CheckGroup> checkGroups = checkGroupService.queryAllCheckGroup();
        if(checkGroups==null){
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroups);
    }
    //    條件查詢+分頁
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult page = checkGroupService.findPage(queryPageBean);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, page);
    }

    //    删除检查组
    @RequestMapping("/deleteCheckGroup")
    public Result deleteCheckGroup(Integer id) throws CustomException {
        System.out.println("join deleteCheckGroup=======>" + id);
        checkGroupService.deleteCheckGroup(id);
        return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }
//    添加检查组
    @RequestMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup,Integer [] checkitemIds){
        System.out.println("join add=====>");
        try {
//            System.out.println("before");
            checkGroupService.add(checkGroup,checkitemIds);
//            System.out.println("after");
            return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
        }
        return  new Result(false,MessageConstant.ADD_CHECKGROUP_FAIL);
    }
//    根据ID查询
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            System.out.println("before1");
            CheckGroup byId = checkGroupService.findById(id);
            System.out.println("after2");
            return  new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,byId);
        }catch (Exception e){
            e.printStackTrace();
        }
        return  new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
    }
    //    根据检查组的ID查找他包含的检查项id
    @RequestMapping("/findCheckItemIdsByGroupId")
    public Result findCheckItemIdsByGroupId(Integer id){
        try {
            List<Integer> ids = checkGroupService.selectItemsByGroupId(id);
            return  new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,ids);
        }catch (Exception e){
            e.printStackTrace();
        }
        return  new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
    }
//    修改
    @RequestMapping("/update")
    public Result update(@RequestBody CheckGroup checkGroup,Integer [] checkitemIds){
        try {
            checkGroupService.update(checkGroup,checkitemIds);
            return  new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
        }
        return  new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
    }
}
